<?php
if (!defined("_INDEX")) { header("Location: /"); die; }

$query = "select `C`.*,count(`V`.`vote_id`) as `votes` from `candidates` as `C`
left join `votes` as `V`
on `C`.`candidate_id` = `V`.`candidate_id`
group by `C`.`candidate_id` order by `C`.`candidate_id`";

$candidates = $db->getTable($query);
$votes = $db->getCell("select count(`vote_id`) from `votes`");

require_once ADMIN.DS."head.php";
?>

<div class="full">
	<div class="submenu_container">
		<a class="submenu_button" href='/admin/report/look'>Просмотр отчетов</a>
		<a class="submenu_button" href='/admin/report/create'>Создать отчет</a>
		<a class="submenu_button" href='/admin/polls/view'>Просмотр опросов</a>
		<a class="submenu_button" href='/admin/polls/create'>Создать опрос</a>
		<a class="submenu_button submenu_button_active" href='/admin/candidates/view'>Кандидаты</a>
	</div>
	<h3>Кандидаты</h3>
	<p>Всего проголосовало: <?php echo $votes; ?> чел.</p><br />
	<table class="message_list">
		<tr class="messages_header"><td width="140px">Кандидат</td><td>Кратко о кандидате</td><td width="150px">Статистика</td><td class='actions'>Действия</td></tr>
		<?php
		foreach ($candidates as $candidate)
		{
			$img = "";
			switch($candidate['candidate_id'])
			{
				case 1: $img = "edro.png"; break;
				case 2: $img = "kprf.png"; break;
				case 3: $img = "spros.png"; break;
				case 4: $img = "ldpr.png"; break;
				case 5: $img = "patr.png"; break;
				case 6: $img = "yabl.png"; break;
				case 7: $img = "prav.png"; break;
			}
			?><tr>
				<td><img src="/files/img/<?php echo $img; ?>" /></td>
				<td><?php echo $candidate['preview']; ?></td>
				<td align='center'><?php echo "<p>Проголосовало:<br />".round($candidate['votes']/$votes*100,2)."% (".$candidate['votes']." чел.)</p><br /><p>Офиц. стат.:<br />".$candidate['wciom_stat']."%</p>"; ?></td>
				<td class='actions'><a href='/admin/candidates/edit/<?php echo $candidate['candidate_id']; ?>' class='send'>Редактировать</a></td>
			</tr><?php
		}
	?></table>
</div>

<?php require_once ADMIN.DS."footer.php"; ?>
